package com.ruihang.generalibrary.ui.util;

import android.content.Context;
import android.support.annotation.DimenRes;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AnimationUtils;

import com.jiongbull.jlog.JLog;

/**
 * Created by Administrator on 2016/12/23.
 */

public class ViewUtils {

    public static void setViewVisible(View view) {
        if (view == null) {
            return;
        }
        view.setVisibility(View.VISIBLE);
    }

    public static void setViewGone(View view) {
        if (view == null) {
            return;
        }
        view.setVisibility(View.GONE);
    }

    public static void setViewsVisible(View... views) {
        if (views == null || views.length == 0) {
            return;
        }
        for (int i = 0; i < views.length; i++) {
            setViewVisible(views[i]);
        }
    }

    public static void setViewsGone(View... views) {
        if (views == null || views.length == 0) {
            return;
        }
        for (int i = 0; i < views.length; i++) {
            setViewGone(views[i]);
        }
    }

    /**
     * 有左右动画效果的显示view
     *
     * @param context
     * @param v
     * @param fromLeft 是否从左边进入，如果为FALSE，则从右边进入
     */
    public static void showWithAnimation(Context context, View v, boolean fromLeft) {
        if (v == null || v.getVisibility() == View.VISIBLE) {
            return;
        }
        v.setVisibility(View.VISIBLE);
        v.setAnimation(AnimationUtils.makeInAnimation(context, fromLeft));
    }

    /**
     * 有左右动画效果的隐藏view
     *
     * @param context
     * @param v
     * @param toRight 是否从右边移出，否则从左边移出
     */
    public static void goneWithAnimation(Context context, View v, boolean toRight) {
        if (v == null || v.getVisibility() == View.GONE) {
            return;
        }
        v.setVisibility(View.GONE);
        v.setAnimation(AnimationUtils.makeOutAnimation(context, toRight));
    }

    public static boolean isTouchPointInView(View view, float x, float y, float statusHeight) {
        if (view == null) {
            return false;
        }
        if (statusHeight < 0) {
            statusHeight = 0;
        }
        int[] location = new int[2];
        view.getLocationInWindow(location);
        float l = location[0];
        float t = location[1] - statusHeight;
        float r = l + view.getMeasuredWidth();
        float b = t + view.getMeasuredHeight();
        return y >= t && y <= b && x >= l && x <= r;
    }

    public static void setViewPaddingRes(View view, @DimenRes int leftRes, int topRes, int rightRes, int bottomRes) {
        if (view == null) {
            return;
        }
        int left = view.getResources().getDimensionPixelOffset(leftRes);
        int top = view.getResources().getDimensionPixelOffset(topRes);
        int right = view.getResources().getDimensionPixelOffset(rightRes);
        int bottom = view.getResources().getDimensionPixelOffset(bottomRes);
        view.setPadding(left, top, right, bottom);
    }

    public static void resetTo169Size(View v) {
        if (v == null) {
            return;
        }
        if (v.getWidth() > 0) {
            resetSizeRatio(v, 16, 9);
        } else {
            v.getViewTreeObserver().addOnGlobalLayoutListener(new ResetRatioGlobal(v, 16, 9));
        }
    }

    public static void resetToSize(View v,float ratioW, float ratioH) {
        if (v == null) {
            return;
        }
        if (v.getWidth() > 0) {
            resetSizeRatio(v, ratioW, ratioH);
        } else {
            v.getViewTreeObserver().addOnGlobalLayoutListener(new ResetRatioGlobal(v, ratioW, ratioH));
        }
    }

    /**
     * 重新设置组件按比例显示
     *
     * @param v
     * @param ratioW
     * @param ratioH
     * @return 是否进行了尺寸设置
     */
    public static boolean resetSizeRatio(View v, float ratioW, float ratioH) {
//        JLog.e("wwsw v="+v+"  w="+ratioW+"  h="+ratioH);
        if (v == null || v.getWidth() == 0) {
            return false;
        }
        float w = v.getWidth();
        int h = v.getHeight();
        if (Math.abs(ratioW / ratioH - w / h) > 0.003) {
            ViewGroup.LayoutParams lp = v.getLayoutParams();
            if (lp != null) {
                h = (int) (w * ratioH / ratioW);
                lp.height = h;
                v.setLayoutParams(lp);
                return true;
            }
        }
        return false;
    }

    private static class ResetRatioGlobal implements ViewTreeObserver.OnGlobalLayoutListener {
        private View tempView;
        private float rw, rh;

        public ResetRatioGlobal(View tempView, float rw, float rh) {
            this.tempView = tempView;
            this.rw = rw;
            this.rh = rh;
        }

        @Override
        public void onGlobalLayout() {
            if (ViewUtils.resetSizeRatio(tempView, rw, rh)) {
                tempView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                tempView = null;
            }
        }
    }
}
